package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.RadioAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.entity.RadioChNameEntity;
import com.lanou.sfu.songforu.entity.RadioEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class RadioFragment extends BaseFragment {
    private View v;
    private GridView gridView;
    private VolleySingleton requestQueue;
    private StringRequest request;
    private Context context;
    private List<RadioEntity.ResultEntity.ListEntity> listEntity;
    private List<RadioChNameEntity.ResultEntity.SonglistEntity> chNameEntity;
    private RadioAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.viewpager_radio, null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String url = Url.RADIO;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RadioEntity radioEntity = gson.fromJson(response, RadioEntity.class);
                listEntity = radioEntity.getResult().getList();
                adapter = new RadioAdapter(listEntity, context);
                gridView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.addQueue(request);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                String url_Ch_name = Url.URL_CHNAME + listEntity.get(position).getCh_name();
                request = new StringRequest(url_Ch_name, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        RadioChNameEntity songlistEntity = gson.fromJson(response, RadioChNameEntity.class);
                        chNameEntity = songlistEntity.getResult().getSonglist();
                        ArrayList<MusicListDatas> list = new ArrayList<>();
                        for (RadioChNameEntity.ResultEntity.SonglistEntity entity : chNameEntity) {
                            MusicListDatas datas = new MusicListDatas();
                            datas.setName(entity.getTitle());
                            datas.setSongId(entity.getSongid());
                            datas.setSinger(entity.getArtist());
                            datas.setFrom(getString(R.string.NET));
                            list.add(datas);
                        }
                        MainActivity.play(list, 0);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                requestQueue.addQueue(request);

                for (RadioEntity.ResultEntity.ListEntity changeType : listEntity) {
                    changeType.setType(0);
                }
                listEntity.get(position).setType(1);
                adapter.notifyDataSetChanged();
            }

        });

    }

    public void init() {
        context = getActivity();
        gridView = (GridView) v.findViewById(R.id.radio_gridView);
        requestQueue = VolleySingleton.getInstance(context);
    }
}
